﻿using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace NP.BPMReportPlatform.BPMPlatform
{
    /// <summary>
    /// BPM平台接口服务
    /// </summary>
    public class BPMPlatformAppService : BPMReportPlatformAppService, IBPMPlatformAppService
    {
        private readonly SqlSugarClient _dbPlatform;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sqlSugarAppService"></param>
        public BPMPlatformAppService(ISqlSugarAppService sqlSugarAppService)
        {
            _dbPlatform = sqlSugarAppService.GetPlatformInstance();
        }

        /// <summary>
        /// 根据工号获取用户GUID
        /// </summary>
        /// <param name="employeeCode"></param>
        /// <returns></returns>
        public async Task<string> GetUserAsync(string employeeCode)
        {
            var userId = await _dbPlatform.Queryable<Users>().With(SqlWith.NoLock).
                Where(x => x.WorkNumber.Equals(CurrentUser.UserName) && x.Status==1).
                Select(x=>x.UserId).SingleAsync();
            if (userId==null || userId==Guid.Empty)
            {
                return "";
            }
            return userId.ToString();
        }

        /// <summary>
        /// 根据用户GUID获取工号
        /// </summary>
        /// <param name="employeeCode"></param>
        /// <returns></returns>
        public string GetUserNumberAsync(Guid userId)
        {
            var userNumber = _dbPlatform.Queryable<Users>().
                Where(x => x.UserId.Equals(userId) && x.Status == 1).
                Select(x => x.WorkNumber).Single();
            if (string.IsNullOrEmpty(userNumber))
            {
                return "";
            }
            return userNumber;
        }
    }
}
